package me.zhengjie.modules.dcompany.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dcompany.service.DependentCompanyService;
import me.zhengjie.modules.dcompany.service.dto.DependentCompanyDto;
import me.zhengjie.modules.dcompany.service.dto.DependentCompanyQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.util.Set;

/**
* @author TianI
* @date 2022-01-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "分公司接口管理管理")
@RequestMapping("/api/dependentCompany")
public class DependentCompanyController {

    private final DependentCompanyService dependentCompanyService;

    @GetMapping
    @Log("查询分公司接口管理")
    @ApiOperation("查询分公司接口管理")
    @PreAuthorize("@el.check('dependentCompany:list')")
    public ResponseEntity query(DependentCompanyQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dependentCompanyService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增分公司接口管理")
    @ApiOperation("新增分公司接口管理")
    @PreAuthorize("@el.check('dependentCompany:add')")
    public ResponseEntity create(@Validated @RequestBody DependentCompanyDto resources){
        return new ResponseEntity<>(dependentCompanyService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改分公司接口管理")
    @ApiOperation("修改分公司接口管理")
    @PreAuthorize("@el.check('dependentCompany:edit')")
    public ResponseEntity update(@Validated @RequestBody DependentCompanyDto resources){
        dependentCompanyService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除分公司接口管理")
    @ApiOperation("删除分公司接口管理")
    @PreAuthorize("@el.check('dependentCompany:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        dependentCompanyService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出分公司接口管理")
    @ApiOperation("导出分公司接口管理")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dependentCompany:list')")
    public void download(HttpServletResponse response, DependentCompanyQueryParam query) throws IOException {
        dependentCompanyService.download(dependentCompanyService.queryAll(query), response);
    }*/

}
